import os
from app.core.img_opencv import *
from app.core.key_board import *

# region 返回游戏主界面
def back_to_main(scale):
    """
        关闭已经打开的窗口，返回游戏主界面
    """
    if (is_image_now('app_img/public_img/close.png', threshold=0.7, scale=scale)):
        while True:
            position=find_grid_patterns('app_img/public_img/close.png', threshold=0.7, scale=scale)
            if(len(position)==0):
                break
            for x,y in position:
                mouse_click(x,y)
        wait_image('app_img/public_img/backpack.png', threshold=0.7, scale=scale)

# region 回到主城
def HomeCity(scale):
    """
    回到主城
    """
    if (if_exist_click_image('app_img/public_img/homecity.png', threshold=0.7, scale=scale)):
        wait_image('app_img/public_img/adventure.png', threshold=0.7, scale=scale)
    elif (is_image_now('app_img/public_img/adventure.png', threshold=0.7, scale=scale)):
        pass
    else:
        return False
    if_exist_click_image('app_img/public_img/look_people.png', threshold=0.9, scale=scale)
    return True
#  region 冒险
def Adventure(scale=1.0):
    """
    进入冒险
    """
    if (if_exist_click_image('app_img/public_img/adventure.png', threshold=0.7, scale=scale)):
        wait_image('app_img/public_img/homecity.png', threshold=0.7, scale=scale)
    elif(is_image_now('app_img/public_img/homecity.png', threshold=0.7, scale=scale)):
        pass
    else:
        return False
    
    return True


# region 点击游戏
def click_game(scale=1.0):
    """
    点击游戏，让键盘可以正常使用
    """
    click_image('app_img/public_img/backpack.png', threshold=0.7, scale=scale)
    wait_image('app_img/scale_match/scale_match_2.png', threshold=0.7, scale=scale)
    back_to_main(scale=scale)


# region 聊天
def chat_with_other(text:str):
    short_press(key='enter')
    copy_text(text=text)
    time.sleep(0.5)
    press_hotkey(key1='ctrl',key2='v')
    short_press(key='enter')

# region 寻找NPC路径
def find_NPC_way(text:str,scale,key_map):

    """
    寻找主城NPC或者事件的路径
    """
    map_event= [
        {
            "传送":"app_img/find_way/map1_0.png",
            "猴长老":"app_img/find_way/map1_1.png",
            "悬赏":"app_img/find_way/map1_2.png",
            "黎山老母":"app_img/find_way/map1_3.png",
            "神秘商人":"app_img/find_way/map1_4.png",
            "猴铁匠":"app_img/find_way/map1_5.png",
            "合成":"app_img/find_way/map1_6.png",
            "公冶灵儿":"app_img/find_way/map1_7.png"
        },
        {
            "传送":"app_img/find_way/map2_0.png", 
            "魏征":"app_img/find_way/map2_1.png",
            "悬赏":"app_img/find_way/map2_2.png",
            "神秘商人":"app_img/find_way/map2_3.png",
            "排行榜":"app_img/find_way/map2_4.png",
            "黑市商人":"app_img/find_way/map2_5.png",
            "仓库":"app_img/find_way/map2_6.png",
            "合成":"app_img/find_way/map2_7.png"
        },
        {
            "传送":"app_img/find_way/map3_0.png",
            "仓库":"app_img/find_way/map3_1.png",
            "瑶池仙女":"app_img/find_way/map3_2.png",
            "悬赏":"app_img/find_way/map3_3.png",
            "排行榜":"app_img/find_way/map3_4.png", 
            "合成":"app_img/find_way/map3_5.png",
            "太白金星":"app_img/find_way/map3_6.png",
            "神秘商人":"app_img/find_way/map3_7.png",
            "黑市商人":"app_img/find_way/map3_8.png"
        },
        {
            "悬赏":"app_img/find_way/map4_0.png",
            "排行榜":"app_img/find_way/map4_3.png",
            "仓库":"app_img/find_way/map4_4.png",
            "太白金星":"app_img/find_way/map4_5.png",
            "合成":"app_img/find_way/map4_6.png", 
            "神秘商人":"app_img/find_way/map4_7.png",
            "黑市商人":"app_img/find_way/map4_8.png"
        }
    ]
    event_index = -1
    map_index = -1
    now_index = -1
    move_position=None
    if is_image_now("app_img/find_way/map1.png", threshold=0.8,scale=scale):
        map_index = 1
        if text in map_event[0]:
            image_path = map_event[0][text]
            if "map1_" in image_path:
                event_index = int(image_path.split("map1_")[1].split(".")[0])
            print("地图一")
    elif is_image_now("app_img/find_way/map2.png", threshold=0.8,scale=scale):
        map_index = 2
        if text in map_event[1]:
            image_path = map_event[1][text]
            if "map2_" in image_path:
                event_index = int(image_path.split("map2_")[1].split(".")[0])
            print("地图二")
    elif is_image_now("app_img/find_way/map3.png", threshold=0.8,scale=scale):
        map_index = 3
        if text in map_event[2]:
            image_path = map_event[2][text]
            if "map3_" in image_path:
                event_index = int(image_path.split("map3_")[1].split(".")[0])
            print("地图三")
    elif is_image_now("app_img/find_way/map4.png", threshold=0.8,scale=scale):
        map_index = 4
        if text in map_event[3]:
            image_path = map_event[3][text]
            if "map4_" in image_path:
                event_index = int(image_path.split("map4_")[1].split(".")[0])
            print("地图四")

    if event_index == -1:
        for i in range(len(map_event)):
            if i == map_index-1:
                continue
            if text in map_event[i]:
                image_path = map_event[i][text]
                map_prefix = f"map{i + 1}_"
                if map_prefix in image_path:
                    event_index = int(image_path.split(map_prefix)[1].split(".")[0])
                map_index = i + 1
                break

        click_image("app_img/find_way/choose_map_0.png",threshold=0.7,scale=scale)
        wait_click_image(f"app_img/find_way/choose_map_{map_index}.png",threshold=0.7,scale=scale)
        click_image("app_img/find_way/choose_map_go.png",threshold=0.7,scale=scale)
    
    map_images = [f"app_img/find_way/map{map_index}_{i}.png" for i in range(15)]
    for image_path in map_images:
        if not os.path.exists(os.path.join(root_dir,image_path)):
            print("找不到图片提前退出了")
            break
        if is_image_now(image_path, threshold=0.7, scale=scale):
            now_index = map_images.index(image_path)
            break
    print(now_index)
    if now_index<event_index and now_index != -1:
        move_position=key_map['key_d']
    elif now_index>event_index and now_index!= -1:
        move_position=key_map['key_a']
    
    click_game(scale=scale)
    short_press(key_map['key_w'],5)
    short_press(key_map['key_s'],2)
    
    image_path = f"app_img/find_way/map{map_index}_{event_index}.png"
    print(image_path)
    for _ in range(70):
        if not move_position is None:
            short_press(move_position,1) 
        if is_image_now(image_path, threshold=0.7, scale=scale):
            break
    if move_position == key_map['key_a']:
        short_press(key_map['key_a'],3)
    wait_click_image(image_path,threshold=0.7,scale=scale)